package com.yf.construction.risk.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yf.base.api.api.dto.PagingReqDTO;
import com.yf.base.utils.BeanMapper;
import com.yf.construction.risk.convert.RiskConvert;
import com.yf.construction.risk.dto.ConstructionRiskDTO;
import com.yf.construction.risk.entity.ConstructionRisk;
import com.yf.construction.risk.mapper.ConstructionRiskMapper;
import com.yf.construction.risk.service.ConstructionRiskService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 业务实现类
 * </p>
 *
 * @author anyone
 * @since 2024-09-13 16:38
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ConstructionRiskServiceImpl extends ServiceImpl<ConstructionRiskMapper, ConstructionRisk> implements ConstructionRiskService {

    @Override
    public Page<ConstructionRiskDTO> paging(PagingReqDTO<ConstructionRiskDTO> reqDTO) {
        ConstructionRisk risk = new ConstructionRisk();
        //查询条件
        QueryWrapper<ConstructionRisk> wrapper = new QueryWrapper<>();
        // 请求参数
        ConstructionRiskDTO params = reqDTO.getParams();
        wrapper.eq(StrUtil.isNotBlank(params.getRiskType()), "risk_type", params.getRiskType());
        //获得数据
        Page<ConstructionRisk> page = this.page(reqDTO.toPage(), wrapper);
        //转换结果
        Page<ConstructionRiskDTO> pageData = RiskConvert.INSTANCE.pageEntityDto(page);
        return pageData;
    }


    @Override
    public void save(ConstructionRiskDTO reqDTO) {
        //复制参数
        ConstructionRisk entity = RiskConvert.INSTANCE.dtoToEntity(reqDTO);
        this.saveOrUpdate(entity);
    }

    @Override
    public void delete(List<String> ids) {
        //批量删除
        this.removeByIds(ids);
    }

    @Override
    public ConstructionRiskDTO detail(String id) {
        ConstructionRisk entity = this.getById(id);
        ConstructionRiskDTO dto = RiskConvert.INSTANCE.entityToDto(entity);
        BeanMapper.copy(entity, dto);
        return dto;
    }

    @Override
    public List<ConstructionRiskDTO> list(ConstructionRiskDTO reqDTO) {
        //分页查询并转换
        QueryWrapper<ConstructionRisk> wrapper = new QueryWrapper<>();
        //转换并返回
        List<ConstructionRisk> list = this.list(wrapper);
        //转换数据
        List<ConstructionRiskDTO> dtoList = RiskConvert.INSTANCE.listEntityDto(list);
        return dtoList;
    }
}
